''' 用户登录管理 '''
from ums import umsservice as db
from flask import Blueprint, jsonify, redirect, url_for, request
import logging, requests
import sys, traceback, os, re
from utils.tokenutil import TokenUtil

logger = logging.getLogger('app')

ums = Blueprint("ums", __name__, url_prefix="/api")

#login
@ums.route('/login',methods=['GET','POST'])
def login():
    formdata = request.json
    logger.info('login',formdata)
    print(formdata)
    param = {}
    param['user_id'] = formdata['username']
    param['password'] = formdata['password']
    result = db.login(param)
    print(result)
    return jsonify(result)

#获取用户信息列表
@ums.route('/user_list',methods=['GET','POST'])
def getuserlist():
    param = {}
    param['pageSize'] = request.args.get('pageSize', default=10, type=int)
    param['pageCurrent']= request.args.get('page', default=1, type=int)
    param['name']= request.args.get('name')
    param['selectRole']= request.args.get('selectRole')
    param['selectStatus']= request.args.get('selectStatus')
    result = {}
    result = db.getuserlist(param)
    print(result)
    return jsonify(result)

#创建用户
@ums.route('/createuser',methods=['GET','POST'])
def createuser():
    formdata = request.json
    logger.info(formdata)
    result = db.createuser(formdata)
    return jsonify(result)

#更新用户
@ums.route('/updateuser',methods=['GET','POST'])
def updateuser():
    formdata = request.json
    logger.info(formdata)
    result = db.updateuser(formdata)
    return jsonify(result)

#删除用户
@ums.route('/deleteuser',methods=['GET','POST'])
def deleteuser():
    formdata = request.json
    logger.info(formdata)
    result = db.deleteuser(formdata)
    return jsonify(result)


#login
@ums.route('/admin_info',methods=['GET','POST'])
def admin_info():
    result = {}
    # 获取请求头中的身份验证令牌  
    token = request.headers.get('Authorization')
    if TokenUtil.verify(token) == 'token已过期' or TokenUtil.verify(token) == '无效的oken':
        result['code'] = 10042
    else:   
        result['code'] = 200
    result['message'] = ''
    adminInfo = {}
    adminInfo['userId'] = '1'
    adminInfo['username'] = 'admin'
    adminInfo['realName'] = 'Admin'
    adminInfo['avatar'] = ''
    adminInfo['desc'] = 'manager'
    adminInfo['password'] = ''
    adminInfo['token'] = 'dfdfdfdsfdsddffsdfhgdshd'
    lpermissions = []
    permission = {}
    permission['label'] = '主控台'
    permission['value'] = 'dashboard_console'
    lpermissions.append(permission)
    permission = {}
    permission['label'] = '监控页'
    permission['value'] = 'dashboard_monitor'
    lpermissions.append(permission)
    permission = {}
    permission['label'] = '工作台'
    permission['value'] = 'dashboard_workplace'
    lpermissions.append(permission)
    adminInfo['permissions'] = lpermissions
    result['result'] = adminInfo
    return jsonify(result)
